package net.kangyee.eventmanager.util;

import java.io.File;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import net.kangyee.eventmanager.EventManager;

public class ConfigManager {

	public String messagePrefix = "&f[&a活动消息&f]";
	public String messageAdminPrefix = "&f[&d活动管理员消息&f]";
	public String messageCreate = "服务器已开启活动:&c%event%&f,大家赶快输入&6join&f报名参加吧!";
	public String messageStop = "活动:&c%event%&f已结束";
	public String messageJoin = "玩家%name%报名参加了活动:&c%event%&f,目前共有&6%count%&f人报名参加活动";
	public String messageQuit = "玩家%name%退出了活动:&c%event%&f,目前共有&6%count%&f人报名参加活动";
	//public String[] messageHelp = {};

	private String configVersion = "";
	public boolean configMetrics = true;
	public boolean configAdminCheckOnJoin = false;
	public boolean configQuitEventOnDeath = false;
	public boolean configQuitEventOnLeave = true;

	private File fileConfig = new File(EventManager.getPluginFolder(), "config.yml");
	private File fileMessage = new File(EventManager.getPluginFolder(), "message.yml");

	private EventManager plugin;

	public ConfigManager(EventManager plugin) {
		this.plugin = plugin;
	}

	// 文件载入=======9
	public void load() {
		if (!fileConfig.exists()) plugin.saveResource("config.yml", false);
		if (!fileMessage.exists()) plugin.saveResource("message.yml", false);

		FileConfiguration config = YamlConfiguration.loadConfiguration(fileConfig);
		this.configVersion = config.getString("config.version", this.configVersion);
		if(!configVersion.equalsIgnoreCase(plugin.getDescription().getVersion()) || configVersion.equals("")){
			plugin.getLogger().info("配置文件版本较低,已替换为新版,旧版将自动备份");
			fileConfig.renameTo(new File(EventManager.getPluginFolder(), "config_OLD_" + configVersion + ".yml"));
			fileMessage.renameTo(new File(EventManager.getPluginFolder(), "message_OLD_" + configVersion + ".yml"));
			plugin.saveResource("config.yml", true);
			plugin.saveResource("message.yml", true);
			config = YamlConfiguration.loadConfiguration(fileConfig);
		}	
		this.configMetrics = config.getBoolean("config.metrics", this.configMetrics);
		this.configAdminCheckOnJoin = config.getBoolean("config.adminCheckOnJoin", this.configAdminCheckOnJoin);
		this.configQuitEventOnDeath = config.getBoolean("config.quitEventOnDeath", this.configQuitEventOnDeath);
		this.configQuitEventOnLeave = config.getBoolean("config.quitEventOnLeave", this.configQuitEventOnLeave);

		FileConfiguration message = YamlConfiguration.loadConfiguration(fileMessage);
		this.messagePrefix = message.getString("message.prefix", this.messagePrefix);
		this.messageAdminPrefix = message.getString("message.adminPrefix", this.messageAdminPrefix);
		this.messageCreate = message.getString("message.create", this.messageCreate);
		this.messageStop = message.getString("message.stop", this.messageStop);
		this.messageJoin = message.getString("message.join", this.messageJoin);
		this.messageQuit = message.getString("message.quit", this.messageQuit);
	}

}
